using System;
using System.Web;
using System.Web.Services;
using System.Xml;
using System.Xml.Xsl;

namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu EngineX.BlogX 
	/// This module is a component of the EngineX blogging and content management engine
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class blogspaceX : x_result {
		#region Private methods
		private x_blogspace blogspacex;
		private x_user userx;
		#endregion

		#region Constructors/Destructors
		/// <summary>Constructor</summary>
		public blogspaceX() : base("blogX") {
			userx = new x_user(Application);
			blogspacex = new x_blogspace(Application);
		}
		#endregion

		#region General web service methods
		/// <summary>Logs a user on to the system</summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		/// <param name="username">login username</param>
		/// <param name="password">login password</param>
		[WebMethod] public XmlDocument Login(string username, string password) {
			try {
				userx.Login(username, password);
				_AddOk("token", userx.Token);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Tests</summary>
		/// <param name="token">authentication token</param>
		[WebMethod] public XmlDocument Test(string token) {
			try {
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		#endregion

		#region Blog manage web service methods
		/// <summary>Get a blogspace</summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Get(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Get");
				blogspacex.Get(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument GetTemplate(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.GetTemplate");
				blogspacex.GetTemplate(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="xsl">the name of the xsl template file</param>
		[WebMethod] public XmlDocument SetTemplate(string token, string name, string xsl) {
			try {
				userx.Validate(token, "Blogspace.SetTemplate");
				blogspacex.SetTemplate(name, xsl);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		[WebMethod] public XmlDocument List(string token) {
			try {
				userx.Validate(token, "Blogspace.List");
				blogspacex.List();
				_AddOk(blogspacex.Blogspaces.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="type">the type of blog in the blogspace</param>
		/// <param name="title">the title of the blogspace</param>
		/// <param name="text">the description of the blogspace</param>
		/// <param name="admin">the username of the blospace administrator</param>
		/// <param name="wanttopics">Flag to indicateif blogspace has topics (yes/no)</param>
		[WebMethod] public XmlDocument Add(string token, string name, string type, string title, string text, string admin, string wanttopics) {
			try {
				userx.Validate(token, "Blogspace.Add");
				blogspacex.Add(name, type, title, text, admin, true, (wanttopics == "yes"));
				blogspacex.Get(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Delete(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Delete");
				blogspacex.Delete(name);
				_AddOk(blogspacex.Blogspaces.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Undelete(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Delete");
				blogspacex.Undelete(name);
				_AddOk(blogspacex.Blogspaces.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Remove(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Remove");
				blogspacex.Remove(name);
				_AddOk(blogspacex.Blogspaces.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="type">the type of blog in the blogspace</param>
		/// <param name="title">the title of the blogspace</param>
		/// <param name="text">the description of the blogspace</param>
		/// <param name="wanttopics">Flag to indicateif blogspace has topics (yes/no)</param>
		[WebMethod] public XmlDocument Edit(string token, string name, string type, string title, string text, string wanttopics) {
			try {
				userx.Validate(token, "Blogspace.Edit");
				blogspacex.Update(name, type, title, text, (wanttopics == "yes"));
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="user">the username of the user</param>
		/// <param name="group">the group (profile) of the user in the blogspace</param>
		[WebMethod] public XmlDocument AddUser(string token, string name, string user, string group) {
			try {
				userx.Validate(token, "Blogspace.AddUser");
				blogspacex.AddUser(name, user, group);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="user">the username of the user</param>
		[WebMethod] public XmlDocument DeleteUser(string token, string name, string user) {
			try {
				userx.Validate(token, "Blogspace.AddUser");
				blogspacex.DeleteUser(name, user);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="title">the title of the topic</param>
		/// <param name="type">the type of blog in the topic</param>
		[WebMethod] public XmlDocument AddTopic(string token, string name, string topic, string title, string type) {
			try {
				userx.Validate(token, "Blogspace.AddTopic");
				blogspacex.AddTopic(name, topic, title, type);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="title">the title of the topic</param>
		/// <param name="type">the type of blog in the topic</param>
		[WebMethod] public XmlDocument EditTopic(string token, string name, string topic, string title, string type) {
			try {
				userx.Validate(token, "Blogspace.EditTopic");
				blogspacex.EditTopic(name, topic, title, type);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		[WebMethod] public XmlDocument DeleteTopic(string token, string name, string topic) {
			try {
				userx.Validate(token, "Blogspace.EditTopic");
				blogspacex.DeleteTopic(name, topic);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		[WebMethod] public XmlDocument Archive(string token, string name, string topic) {
			try {
				userx.Validate(token, "Blog.Archive");
				topic = (topic == "")? "default" : topic;
				blogspacex.Archive(name, topic, false);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument GetArchive(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Archive");
				_AddOk(String.Concat("<archivetype>", blogspacex.GetArchive(name), "</archivetype>"));
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="type">the type of blog in the blogspace</param>
		[WebMethod] public XmlDocument ChangeArchive(string token, string name, string type) {
			try {
				userx.Validate(token, "Blogspace.Archive");
				blogspacex.ChangeArchive(name, type);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Index(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Index");
				blogspacex.Index(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="text">the description of the link</param>
		/// <param name="url">the url to link to</param>
		[WebMethod] public XmlDocument AddLink(string token, string name, string text, string url) {
			try {
				userx.Validate(token, "Blogspace.ManageLinks");
				blogspacex.AddLink(name, text, url);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="id">the link identifier</param>
		/// <param name="text">the description of the link</param>
		/// <param name="url">the url to link to</param>
		[WebMethod] public XmlDocument EditLink(string token, string name, int id, string text, string url) {
			try {
				userx.Validate(token, "Blogspace.ManageLinks");
				blogspacex.EditLink(name, id, text, url);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="id">the link identifier</param>
		[WebMethod] public XmlDocument DeleteLink(string token, string name, int id) {
			try {
				userx.Validate(token, "Blogspace.ManageLinks");
				blogspacex.DeleteLink(name, id);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="filename">the name of the image file</param>
		[WebMethod] public XmlDocument AddImage(string token, string name, string topic, string filename) {
			try {
				userx.Validate(token, "Blogspace.AddImage");
				blogspacex.AddImage(name, topic, filename);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="author">the username of the blog author</param>
		/// <param name="title">the title of the blog</param>
		/// <param name="image">the image to be associated with the blog</param>
		/// <param name="html">the html content for the blog</param>
		[WebMethod] public XmlDocument AddBlog(string token, string name, string topic, string author, string title, string image, string html) {
			try {
				userx.Validate(token, "Blogspace.EditTopic");
				author = (author == "" || author == null)? userx.Username : author;
				blogspacex.AddBlog(name, topic, author, title, image, html);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		[WebMethod] public XmlDocument GetBlog(string token, string name, string topic) {
			try {
				userx.Validate(token, "Blogspace.Get");
				blogspacex.GetBlog(name, topic);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument CountBlogs(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Get");
				blogspacex.CountBlogs(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Current(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Recent");
				blogspacex.Current(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/*
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		/// <param name="topic">the name of the topic</param>
		/// <param name="howmany">the count of recent blogs</param>
		[WebMethod] public XmlDocument Recent(string token, string name, string topic, int howmany) {
			try {
				userx.Validate(token, "Blogspace.Recent");
				if (topic == "" || topic == "null" || topic == "*")
					blogspacex.Recent(name, howmany);
				else
					blogspacex.Recent(name, topic, howmany);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Recent(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Recent");
				blogspacex.Recent(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary></summary>
		/// <param name="token">authentication token</param>
		/// <param name="name">the name of the blogspace</param>
		[WebMethod] public XmlDocument Comment(string token, string name) {
			try {
				userx.Validate(token, "Blogspace.Comment");
				blogspacex.Comment(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		*/
		#endregion

	}
}
